package com.tgsoft.kdcloud.controller;

import com.tgsoft.security.utils.TokenUtils;
import com.tgsoft.common.core.util.Result;
import com.tgsoft.kdcloud.service.SysAuthService;
import com.tgsoft.kdcloud.vo.AccessTokenVO;
import com.tgsoft.kdcloud.vo.SysAccountLoginVO;
import com.tgsoft.kdcloud.vo.SysUserTokenVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AllArgsConstructor;

import org.springframework.web.bind.annotation.*;

/**
 * 认证管理
 *
 * @author 阿沐 babamu@126.com
 * <a href="https://maku.net">MAKU</a>
 */
@RestController
@RequestMapping("token")
@Tag(name = "认证管理")
@AllArgsConstructor
public class SysAuthController {

    private final SysAuthService sysAuthService;


    @PostMapping("login")
    @Operation(summary = "账号密码登录")
    public Result<SysUserTokenVO> login(@RequestBody SysAccountLoginVO login) {
        SysUserTokenVO token = sysAuthService.loginByAccount(login);

        return Result.ok(token);
    }


    @PostMapping("token")
    @Operation(summary = "获取 accessToken")
    public Result<AccessTokenVO> token(String refreshToken) {
        AccessTokenVO token = sysAuthService.getAccessToken(refreshToken);

        return Result.ok(token);
    }

    @PostMapping("logout")
    @Operation(summary = "退出")
    public Result<String> logout(HttpServletRequest request) {
        sysAuthService.logout(TokenUtils.getAccessToken(request));

        return Result.ok();
    }

}
